SHELL_FILES_DIRECTORIES ?= .
SHELL_FILES_ALL ?= $(shell \
	find $(SHELL_FILES_DIRECTORIES) -type f -perm /a+x \
		| grep -vE "/(\.git|debian/.*)/" \
		| xargs grep -lI '^\#!/bin/.*sh' \
)

SHELL_FILES_DASH ?= $(shell echo "$(SHELL_FILES_ALL)" | xargs grep -l '^\#!/bin/sh$$')
SHELL_FILES_BASH ?= $(shell echo "$(SHELL_FILES_ALL)" | xargs grep -l '^\#!/bin/bash$$')
SHELLCHECK_BIN ?= $(shell which shellcheck)
# we allow external scripts to be sourced
SHELLCHECK_ARGS ?= -x


.PHONY: lint
lint: lint-shell

.PHONY: lint-shell
lint-shell:
	@if [ -z "$(SHELLCHECK_BIN)" ]; then echo >&2 "Missing 'shellcheck' command. Maybe you want to disable checks via DISABLE_SHELL_CHECK=1."; false; fi
	@if [ -n "$(SHELL_FILES_BASH)" ]; then "$(SHELLCHECK_BIN)" $(SHELLCHECK_ARGS) $(SHELL_FILES_BASH); fi
	@if [ -n "$(SHELL_FILES_DASH)" ]; then "$(SHELLCHECK_BIN)" --shell dash $(SHELLCHECK_ARGS) $(SHELL_FILES_DASH); fi
